package com.vy.stl.interceptor;

import com.vy.stl.constants.Constants;
import com.vy.stl.entity.Member;
import com.vy.stl.entity.Reserve;
import com.vy.stl.service.MemberService;
import com.vy.stl.service.ReserveService;
import com.vy.stl.util.JWTUtil;
import com.vy.stl.util.Result;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @create: 2025/09/10 16:28
 * @description:
 */
@RestController("/foreReserveController")
@RequestMapping( value = "/fore")
@Slf4j
public class ReserveController {

    @Autowired
    private ReserveService reserveService;
    @Autowired
    private MemberService memberService;

    @RequestMapping("/reserve")
    public Result remove(@RequestBody Reserve reserve,HttpServletRequest request) {
        //获取请求头中的token
        String token = request.getHeader("token");
        try {
            Claims claims = JWTUtil.parseJWT(token);
            String phone = claims.getSubject();
            //根据手机号获取会员信息
            Member loginMember = memberService.getMemberByPhone(phone);
            if (!ObjectUtils.isEmpty(loginMember)) {
                reserve.setMemberId(loginMember.getId());
                reserveService.add(reserve);
            }
            return Result.success(Constants.STATUS_200);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(Constants.ADD_RESERVE_ERROR,e.getMessage());
        }
    }
}
